<template>
  <section>
    <!--高级查询工具条-->
    <el-col :span="24" class="toolbar" style="padding-bottom: 0px;">
      <el-form :inline="true" :model="filters">
        <el-form-item>
          <el-input v-model="filters.keyword" placeholder="请输入关键字"></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" v-on:click="search">查询</el-button>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="handleAdd">新增</el-button>
        </el-form-item>
      </el-form>
    </el-col>

    <!--列表展示-->
    <el-table @row-dblclick="openDrawer" :data="type" highlight-current-row v-loading="listLoading" @selection-change="selsChange" style="width: 100%;">
      <el-table-column type="selection" width="55">
      </el-table-column>
      <el-table-column prop="id" label="编号" width="300" sortable>
      </el-table-column>
      <el-table-column prop="name" label="操作" width="300"  sortable>
      </el-table-column>
      <el-table-column prop="sn" label="类型" width="300"  sortable>
      </el-table-column>
      <el-table-column label="操作" width="400">
        <template scope="scope">
          <el-button size="small" @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button type="danger" size="small" @click="handleDel(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- 抽屉 -->
    <el-drawer
        title="抽屉"
        :visible.sync="table"
        direction="rtl"
        size="50%">
      <el-table :data="details">
        <el-table-column property="id" label="编号" width="200"></el-table-column>
        <el-table-column property="name" label="类型" width="200"></el-table-column>
        <el-table-column label="操作" width="150">
          <template scope="scope">
            <el-button size="small" @click="drawerEdit(scope.$index, scope.row)">编辑</el-button>
            <el-button type="danger" size="small" @click="drawerDel(scope.$index, scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-drawer>

    <!--分页工具条-->
    <el-col :span="24" class="toolbar">
      <el-button type="danger" @click="batchRemove" :disabled="this.sels.length===0">批量删除</el-button>
      <el-pagination layout="prev, pager, next" @current-change="handleCurrentChange"
                     :page-size="pageSize"
                     :total="total"
                     :current-page="currentPage"
                     style="float:right;">
      </el-pagination>
    </el-col>

    <!--编辑界面-->
    <el-dialog :title="title" :visible.sync="editFormVisible" :close-on-click-modal="false">
      <el-form :model="editForm" label-width="80px" :rules="editFormRules" ref="editForm">
        <el-form-item label="类型" prop="name">
          <el-input v-model="editForm.name" auto-complete="off"></el-input>
        </el-form-item>
        <el-form-item label="编号" prop="sn">
          <el-input v-model="editForm.sn" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click.native="editFormVisible = false">取消</el-button>
        <el-button type="primary" @click.native="editSubmit" :loading="editLoading">提交</el-button>
      </div>
    </el-dialog>

  </section>
</template>

<script>
import util from '../../common/js/util'
//import NProgress from 'nprogress'
import { getUserListPage, removeUser, batchRemoveUser, editUser, addUser } from '../../api/api';

export default {
  data() {
    return {
      /* 关闭抽屉 */
      /* 抽屉的数据 */
      details:[],
      title:"",
      filters: {
        keyword: ''
      },
      //抽屉

      dialog: false,
      loading: false,
      form: {
        name: '',
        region: '',
        date1: '',
        date2: '',
        delivery: false,
        type: [],
        resource: '',
        desc: ''
      },
      formLabelWidth: '80px',
      timer: null,
      type: [],
      detail:[],
      total: 0, //总条数初始值
      currentPage: 1, //开始页
      pageSize:5, //每页展示条数
      listLoading: false,
      sels: [],//列表选中列

      editFormVisible: false,//编辑界面是否显示
      editLoading: false,
      editFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
      //编辑界面数据
      editForm: {
        id: null,
        name: '',
        sn:'',
      },

      addFormVisible: false,//新增界面是否显示
      addLoading: false,
      addFormRules: {
        name: [
          { required: true, message: '请输入姓名', trigger: 'blur' }
        ]
      },
    }
  },
  methods: {
    search(){//高级查询
      // 定位到第一页
      this.currentPage = 1;
      //调用查询刷新页面
      this.getSystemdictionarytype();
    },
    handleCurrentChange(val) {//分页
      this.currentPage = val;
      this.getSystemdictionarytype();
    },
    //获取数据字典列表
    getSystemdictionarytype() {//展示列表数据
      //请求参数
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword//高级查询关键字
      };
      this.listLoading = true;//开启
      //NProgress.start();
      this.$http.post("/type",para)//调用接口，数据源
          .then(result=>{
            this.listLoading = false;//关闭
            console.log(result);
            result = result.data;//PageList
            this.type = result.rows;//查询到的数据
            this.total = result.total;//总条数
          })
          .catch(result=>{
            console.log(result);
          })
    },
    //获取数据字典明细列表
    getSystemdictionarydetail() {//展示列表数据
      //请求参数
      let para = {
        currentPage: this.currentPage,
        pageSize: this.pageSize,
        keyword: this.filters.keyword//高级查询关键字
      };
      this.listLoading = true;//开启
      //NProgress.start();
      this.$http.post("/detail",para)//调用接口，数据源
          .then(result=>{
            this.listLoading = false;//关闭
            console.log(result);
            result = result.data;//PageList
            this.detail = result.rows;//查询到的数据
            this.total = result.total;//总条数
          })
          .catch(result=>{
            console.log(result);
          })

    },

    //数据字典删除
    handleDel: function (index, row) {
      this.$confirm('确认删除该记录吗?', '亲亲友情提示', {
        type: 'warning'
      }).then(() => {// 确定按钮
        this.listLoading = true;//开启
        this.$http.delete("/type/"+row.id)
            .then(result=>{
              this.listLoading = false;//关闭
              result = result.data;
              if(result.success){
                this.$message({//后端删除数据成功
                  message: result.message,//删除成功的展示信息
                  type: 'success'
                });
              }else{
                this.$message({//后端删除失败
                  message: result.message,//删除失败的展示信息
                  type: 'error'
                });
              }
              this.currentPage = 1;//定位到第一页
              this.getSystemdictionarytype();//重新刷新页面
            })
            .catch(result=>{//调用后端错误
              this.$message({
                message: '系统繁忙，稍后重试',
                type: 'error'
              });
            })
      }).catch(() => {
      });
    },
    //删除
    handleDel2: function (index, row) {
      this.$confirm('确认删除该记录吗?', '亲亲友情提示', {
        type: 'warning'
      }).then(() => {// 确定按钮
        this.listLoading = true;//开启
        this.$http.delete("/detail/"+row.id)
            .then(result=>{
              this.listLoading = false;//关闭
              result = result.data;
              if(result.success){
                this.$message({//后端删除数据成功
                  message: result.message,//删除成功的展示信息
                  type: 'success'
                });
              }else{
                this.$message({//后端删除失败
                  message: result.message,//删除失败的展示信息
                  type: 'error'
                });
              }
              this.currentPage = 1;//定位到第一页
              this.getSystemdictionarydetail();//重新刷新页面
            })
            .catch(result=>{//调用后端错误
              this.$message({
                message: '系统繁忙，稍后重试',
                type: 'error'
              });
            })
      }).catch(() => {
      });
    },
    //显示编辑界面
    handleEdit: function (index, row) {
      this.title = "编辑";
      this.editFormVisible = true;
      this.editForm = Object.assign({}, row);
    },
    //显示新增界面
    handleAdd: function () {
      this.title = "新增";
      this.editFormVisible = true;//开启弹出框
      this.editForm = {//表单重置
        id: null,
        name: '',
        sn: '',
      };
    },
    //编辑
    editSubmit: function () {
      this.$refs.editForm.validate((valid) => {
        if (valid) {
          this.$confirm('确认提交吗？', '提示', {}).then(() => {
            this.editLoading = true;
            //NProgress.start();m
            let para = Object.assign({}, this.editForm);
            //para.birth = (!para.birth || para.birth == '') ? '' : util.formatDate.format(new Date(para.birth), 'yyyy-MM-dd');
            this.$http.put("type",para)
                .then(result=>{
                  this.editLoading = false;//关闭按钮忙等框
                  this.editFormVisible = false;//关闭弹出框
                  result = result.data;
                  if(result.success){//后端删除成功
                    this.$message({
                      message: result.message,
                      type: 'success'
                    });
                  }else{//后端删除失败
                    this.$message({
                      message: result.message,
                      type: 'error'
                    });
                  }
                  this.$refs['editForm'].resetFields();
                  this.editFormVisible = false;
                  this.getSystemdictionarytype();
                });

          })
              .catch(result=>{

              })

        }
      });
    },
    selsChange: function (sels) {
      this.sels = sels;
    },
    //批量删除
    batchRemove: function () {
      var ids = this.sels.map(item => item.id);
      this.$confirm('确认删除该选中记录吗？', '亲亲友好提示一下', {
        type: 'warning'
      }).then(() => {//确定要删除
        this.listLoading = true;
        this.$http.patch("/type",ids)
            .then(result=>{
              this.listLoading = false;//关闭忙等框
              result = result.data;
              if(result.success){//后端删除成功
                this.$message({
                  message: result.message,//删除成功的展示信息
                  type: 'success'
                });
              }else{//后端删除失败
                this.$message({
                  message: result.message,//删除失败的展示信息
                  type: 'error'
                });
              }
              this.currentPage = 1;//定位到第一页
              this.getSystemdictionarytype();//重新刷新页面
            })
            .catch(result=>{//调用后端调用不同
              console.log(result);
              this.$message({
                message: '系统繁忙，稍后重试',
                type: 'error'
              });
            })
      }).catch(() => {//取消

      });
    },
  },
  mounted() {
    this.getSystemdictionarytype();
    this.getSystemdictionarydetail();
  }
}

</script>

<style scop/>